package com.ibmeeting.modules.security.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

@Component
public class VerificationCodeAuthenticationProvider implements AuthenticationProvider {
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        VerificationCodeAuthenticationToken token =
                (VerificationCodeAuthenticationToken) authentication;

/*        // 1. 验证码校验
        if (!codeService.validate(
                token.getPrincipal().toString(),
                token.getCredentials().toString(),
                token.getLoginType())) {
            throw new BadCredentialsException("Invalid verification code");
        }

        // 2. 加载用户
        UserDetails user = switch (token.getLoginType()) {
            case PHONE -> userDetailsService.loadUserByPhone(token.getPrincipal().toString());
            case EMAIL -> userDetailsService.loadUserByEmail(token.getPrincipal().toString());
        };*/

        // 3. 返回已认证Token
/*        return new VerificationCodeAuthenticationToken(
                user.getUsername(),
                user.getAuthorities(),
                token.getLoginType()
        );*/

        return new VerificationCodeAuthenticationToken(
                "1232",
                "user.getAuthorities()",
                VerificationCodeAuthenticationToken.LoginType.EMAIL
        );

    }

    @Override
    public boolean supports(Class<?> authentication) {
        return VerificationCodeAuthenticationToken.class.isAssignableFrom(authentication);
    }
}